<?php
class ClsClient extends ClsNaanalController
{
	/**
         * request
         * @package naanal
         * @var string 
         */
        public $request=null;
        /**
         * get
         * @package naanal
         * @var type 
         */
	public $get=null;
        /**
         * post
         * @package naanal
         * @var string 
         */
	public $post=null;
        /**
         * files
         * @package naanal
         * @var string 
         */
	public $files=null;
        /**
         * pdo
         * @package naanal
         * @var string 
         */
	public $pdo=null;
        /**
         * controlpanel
         * @var string 
         */
	public $controlpanel=null;
	//public $mode="create";
	//private $redirect=null; 
	/**
         * construct
         * @package naanal
         * @param string $controlpanel
         */
	function __construct()
	{
            parent::__construct();
	}
	/**
         * activate
         * @package naanal
         */
	function activate()
	{    
		$this->mode="activate";
		$objUser=new ClsWClient();
		$objUser->set_id($this->request["id"]);
		$objUser->set_active(1);
  		$sql="update `client` set active=:active where `id`=:id";
  		$this->pdo->setQuery($sql,$objUser);
  		$this->redirect="{$this->urlModuleParam}=client&{$this->urlActionParam}=list";
	}
        /**
         * deactivate
         * @package naanal
         */
	function deactivate()
	{
		$this->mode="deactivate";
		$objUser=new ClsWClient();
		$objUser->set_id($this->request["id"]);
		$objUser->set_active(0);
  		$sql="update `client` set active=:active where `id`=:id";
  		$this->pdo->setQuery($sql,$objUser);
  		$this->redirect="{$this->urlModuleParam}=client&{$this->urlActionParam}=list";
	}
	/**
         * insert
         * @package naanal
         * @return array 
         */
        
	function insert()
	{     
		$this->mode="insert";
		
		$sql="select * from client where email='{$_REQUEST["email"]}'";
		$this->pdo->setQuery($sql);
		$arr=$this->pdo->getAllAssoc();
		if($arr)
		{
			errMsg("CLIENT_INSERT_EMAIL_EXIST");
			$objUser=new ClsWClient();
			$objUser->setArray($_REQUEST);
			$this->mode="create";
			//$this->restart=true;
			return array("form"=>$objUser);
		}             
		
		$sql="insert into `client` (`company`,`address1`,`address2`,`city`,`country`,`state`,`logo`,`created`,`modified`,`email`) values (:company,:address1,:address2,:city,:country,:state,:logo,'".time()."','".time()."',:email)";
		$obj=new ClsWClient();
		$obj->setArray($this->request);
      	$this->pdo->setQuery($sql,$obj);
      	$clientid=$this->pdo->lastInsertId();
      	
      	$obj=new ClsWUser();
      	$obj->set_userid($this->request["userid"]);
      	$obj->set_name($this->request["name"]);
      	$obj->set_password($this->request["password"]);
      	$obj->set_client_id($clientid);
      	$obj->set_isadmin(1);//trace($obj);
      	$sql="insert into `user` (`userid`,`password`,`isadmin`,`client_id`,`name`,`active`) values(:userid,:password,:isadmin,:client_id,:name,'1')";
      	$this->pdo->setQuery($sql,$obj);
      	$insid=$this->pdo->lastInsertId();
      	
      	$sql="update `client` set `defaultuser`='$insid' where id=".$clientid;
      	$this->pdo->setQuery($sql);
      	
      	$sql="select name from role";
      	$this->pdo->setQuery($sql);
      	$arrObj=array();
      	while($obj=$this->pdo->getObject("ClsWRole"))
      	{
      		$arrObj[]=$obj;
      	}
      	foreach($arrObj as $ind=>$obj)
      	{
      		$sql="insert into clientrole (`name`,`client_id`) values('{$obj->get_name()}','{$clientid}')";	
      	}
      	
		$sql="select permission_id,role_id from rolepermission";
      	$this->pdo->setQuery($sql);
      	$arrObj=array();
      	while($obj=$this->pdo->getObject("ClsWRolepermission"))
      	{
      		$arrObj[]=$obj;
      	}
      	foreach($arrObj as $ind=>$obj)
      	{
      		$sql="insert into clientrolepermission (`permission_id`,`role_id`,`client_id`) values('{$obj->get_permission_id()}','{$obj->get_role_id()}','{$clientid}')";	
      	}
      	errMsg("CLIENT_INSERT_SUCCESS");
      	$this->redirect="{$this->urlModuleParam}=client&{$this->urlActionParam}=list";
	}
        /**
         * upload
         * @package naanal
         */
	function upload()
	{
		$path=ClsConfig::$UPLOAD_FILE_PATH."logo/{$this->id}/";
                $webpath=ClsConfig::$UPLOAD_WEB_PATH."logo/{$this->id}/";
		if(!file_exists($path))
		{
			mkdir($path,0777,true);
		}
		move_uploaded_file($this->files["upload"]["tmp_name"], $path.$this->files["upload"]["name"]);
		$this->request["logo"]="logo/{$this->id}/".$this->files["upload"]["name"]; 
	}
        /**
         * resetpass
         * @package naanal
         */
	function resetpass()
	{
		$this->mode="resetpass";
		
		$sql="select * from `client` where id=".$this->request["id"];
	    $this->objPDO->setQuery($sql);
	    $obj=$this->objPDO->getObject("ClsWClient");
	    $defaultuser=$obj->get_defaultuser();
	    $email=$obj->get_email();
	    
	    $newpass=getRandomString(6);
	    $this->pdo->setQuery("update `user` set `password`='$newpass' where id=".$defaultuser);
	    
	    $msg='Your new password is '.$newpass;
	    if(@mail($email,"Password Reset",$msg,'from:info@unicomtech.in'))
	    {
	    	errMsg("CLIENT_RESET_PASSWORD_SUCCESS");
	    	$this->redirect="{$this->urlModuleParam}=client&{$this->urlActionParam}=list";	
	    }
	    else
	    {
	    	errMsg("CLIENT_RESET_PASSWORD_FAIL");
	    	$this->redirect="{$this->urlModuleParam}=client&{$this->urlActionParam}=list";
	    }
	}
	/**
         * update
         * @package naanal
         */
	function update()
	{
            $this->mode="update";
            $sql="update `client` set company=:company,address1=:address1,address2=:address2,city=:city,country=:country,state=:state,logo=:logo,email=:email where `id`=:id";
            $objUser=new ClsWClient();
            $objUser->setArray($this->request);
	    $this->pdo->setQuery($sql,$objUser);
	    $this->redirect="{$this->urlModuleParam}=client&{$this->urlActionParam}=list";
	}
	/**
         * edit
         * @package naanal
         * @return array 
         */
	function edit()
	{
		$this->mode="edit";
		$sql="select * from client where id='{$this->request["id"]}'";
		$this->pdo->setQuery($sql);
		$objUser=new ClsWClient();
		$objUser=$this->pdo->getObject("ClsWClient");
		return array("form"=>$objUser);
	}
	/**
         * create 
         * @package naanal
         * @return array 
         */
	function create()
	{
		$this->mode="create";
		$objUser=new ClsWClient();
		return array("form"=>$objUser);
	}
        /**
         * delete
         * @package naanal
         */
	function delete()
	{
		$this->mode="delete";
		$objUser=new ClsWClient();
		$objUser->set_id($this->request["id"]);
  		$sql="delete from `client` where `id`=:id";
  		$this->pdo->setQuery($sql,$objUser);
  		$this->redirect="{$this->urlModuleParam}=client&{$this->urlActionParam}=list";
	}
	/**
         * show_list
         * @package naanal
         * @return array 
         */
	function show_list()
	{
		$this->mode="list";
                $arrObj=$this->showList();
		/*$sql="select * from client";
		$this->pdo->setQuery($sql);
		$arrObj=$this->pdo->getAllObject("ClsWClient");*/
		return $arrObj;		
	}
}
?>